package com.das.mechanic_base.adapter.main;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.List;

public class X3EditFaultLightAdapter extends RecyclerView.Adapter<X3EditFaultLightAdapter.EditFaultLightHolder> {

    private Context mContext;

    private List<String> mList;

    private List<String> sList;

    public X3EditFaultLightAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
        this.sList = new ArrayList<>();
    }

    public void setList(List<String> mList, List<String> sList) {
        this.mList = mList;
        this.sList = sList;
        notifyDataSetChanged();
    }

    public List<String> getsList() {
        return sList;
    }

    @NonNull
    @Override
    public EditFaultLightHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new EditFaultLightHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_item_edit_fault_light,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull EditFaultLightHolder holder, int position) {
        String itemName = mList.get(position);

        if (X3StringUtils.isEmpty(itemName)) {
            return;
        }

        String[] itemNames = itemName.split("_");

        if (itemNames == null || itemNames.length < 2) {
            return;
        }

        holder.tv_name.setText(itemNames[1]);

        if(sList.contains(itemName)){
            holder.iv_select.setImageResource(R.mipmap.x3_edit_light_select);
        }else {
            holder.iv_select.setImageResource(R.mipmap.x3_edit_light_default);
        }

        switch (itemNames[0]) {
            case "1":
                holder.iv_photo.setImageResource(R.mipmap.x3_light_break_error);
                break;
            case "2":
                holder.iv_photo.setImageResource(R.mipmap.x3_light_warn);
                break;
            case "3":
                holder.iv_photo.setImageResource(R.mipmap.x3_light_oil_error);
                break;
            case "4":
                holder.iv_photo.setImageResource(R.mipmap.x3_light_battery_error);
                break;
            case "5":
                holder.iv_photo.setImageResource(R.mipmap.x3_light_abs_error);
                break;
            case "6":
                holder.iv_photo.setImageResource(R.mipmap.x3_light_engine_error);
                break;
            case "7":
                holder.iv_photo.setImageResource(R.mipmap.x3_light_water_error);
                break;
            case "8":
                holder.iv_photo.setImageResource(R.mipmap.x3_light_fuel_oil_error);
                break;
            case "9":
                holder.iv_photo.setImageResource(R.mipmap.x3_light_tyre_error);
                break;
            case "10":
                holder.iv_photo.setImageResource(R.mipmap.x3_light_safe_error);
                break;
            case "11":
                holder.iv_photo.setImageResource(R.mipmap.x3_light_glass_water_error);
                break;
            case "12":
                holder.iv_photo.setImageResource(R.mipmap.x3_light_turn_error);
                break;
            case "13":
                holder.iv_photo.setImageResource(R.mipmap.x3_light_esp_error);
                break;
            case "14":
                holder.iv_photo.setImageResource(R.mipmap.x3_light_epc_error);
                break;
            case "15":
                holder.iv_photo.setImageResource(R.mipmap.x3_others);
                break;
        }

        holder.itemView.setOnClickListener(v -> {
            if (sList.contains(mList.get(position))) {
                sList.remove(mList.get(position));
            }else {
                sList.add(mList.get(position));
            }
            notifyItemChanged(position);
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class EditFaultLightHolder extends RecyclerView.ViewHolder {

        ImageView iv_photo;
        TextView tv_name;
        ImageView iv_select;

        public EditFaultLightHolder(@NonNull View itemView) {
            super(itemView);
            iv_photo=itemView.findViewById(R.id.iv_photo);
            tv_name=itemView.findViewById(R.id.tv_name);
            iv_select=itemView.findViewById(R.id.iv_select);
        }
    }

}
